package user.view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import user.model.User;
import user.util.StringUtil;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JMenu UserNameText;
	private static String loginName = null;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
			


	public MainFrame() {
		setTitle("用户主界面");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1300, 1000);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		UserNameText = new JMenu("用户名");
		loginName = LoginFrame.userName();
		UserNameText.setText(loginName);
		UserNameText.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
		UserNameText.setIcon(new ImageIcon(MainFrame.class.getResource("/images/用户2.png")));
		menuBar.add(UserNameText);
		
		
		JMenuItem menuItem1 = new JMenuItem("我的书架");
		menuItem1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MyBook myBook = new MyBook();
				myBook.setVisible(true);
				table.add(myBook);
			}
		});
		menuItem1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		menuItem1.setIcon(new ImageIcon(MainFrame.class.getResource("/images/书架.png")));
		UserNameText.add(menuItem1);
		
		JMenuItem menuItem2 = new JMenuItem("修改密码");
		menuItem2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MessageUpdate messageUpdate = new MessageUpdate();
				messageUpdate.setVisible(true);
			}
		});
		menuItem2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		menuItem2.setIcon(new ImageIcon(MainFrame.class.getResource("/images/修改.png")));
		UserNameText.add(menuItem2);
		
		JMenuItem menuItem3 = new JMenuItem("退出登录");
		menuItem3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = JOptionPane.showConfirmDialog(null, "是否退出登录");
				if(x==0) {//退出为0
					new LoginFrame().setVisible(true);
					dispose();
				}
				
			}
		});
		menuItem3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		menuItem3.setIcon(new ImageIcon(MainFrame.class.getResource("/images/退出.png")));
		UserNameText.add(menuItem3);
		
		JMenu menu = new JMenu("图书管理");
		menu.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
		menu.setIcon(new ImageIcon(MainFrame.class.getResource("/images/记事本.png")));
		menuBar.add(menu);
		
		JMenuItem menuItem4 = new JMenuItem("图书查询");
		menuItem4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookSelectFrame bookSelectFrame = new BookSelectFrame();
				bookSelectFrame.setVisible(true);
				table.add(bookSelectFrame);
			}
		});
		menuItem4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		menuItem4.setIcon(new ImageIcon(MainFrame.class.getResource("/images/查询.png")));
		menu.add(menuItem4);
		
		JMenuItem menuItem6 = new JMenuItem("借书历史");
		menuItem6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookHistoryFrame bookHistoryFrame = new BookHistoryFrame();
				bookHistoryFrame.setVisible(true);
				table.add(bookHistoryFrame);
			}
		});
		menuItem6.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		menuItem6.setIcon(new ImageIcon(MainFrame.class.getResource("/images/历史 (1).png")));
		menu.add(menuItem6);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		table = new JTable();
		table.setBackground(new Color(153, 204, 153));
		
		JButton btnNewButton = new JButton("图书查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookSelectFrame bookSelectFrame = new BookSelectFrame();
				bookSelectFrame.setVisible(true);
				table.add(bookSelectFrame);
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button = new JButton("我的书架");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MyBook myBook = new MyBook();
				myBook.setVisible(true);
				table.add(myBook);
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_1 = new JButton("借书历史");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookHistoryFrame bookHistoryFrame = new BookHistoryFrame();
				bookHistoryFrame.setVisible(true);
				table.add(bookHistoryFrame);
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(table, GroupLayout.PREFERRED_SIZE, 1094, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE)
						.addComponent(button, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(160)
					.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
					.addGap(154)
					.addComponent(button, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
					.addGap(155)
					.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(247, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(111, Short.MAX_VALUE)
					.addComponent(table, GroupLayout.PREFERRED_SIZE, 806, GroupLayout.PREFERRED_SIZE))
		);
		this.setLocationRelativeTo(null);
		contentPane.setLayout(gl_contentPane);
		
		ImageIcon icon = new ImageIcon("src/images/main.png");
		JLabel background = new JLabel(icon);
		this.getLayeredPane().setLayout(null);
		contentPane.setOpaque(false);
		background.setBounds(0,0,1300,1000);
		this.getLayeredPane().add(background,new Integer(Integer.MIN_VALUE));
	}
}
